package com.szholly.pro.watergas_server.ps.schedule;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.szholly.plug.form.excel.utils.StringUtil;
import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.data.DbManager;
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import lrq.common.MyLog;
import lrq.data.DataTable;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;

public class MonitorDataJob implements Job{

	private static Jedis  redis = new Jedis("58.251.161.70", 6379);
	private static  Pipeline pl = redis.pipelined();
	 /**
	  * 实时数据定时更新
	  * @param arg0
	  * @throws JobExecutionException
	  */
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		//Logger log = MyLog.getLogger("MonitorDataJob");//日志名称
		try {
			long start = System.currentTimeMillis();
			long end = System.currentTimeMillis(); 
			String sql="SELECT t2.name as STATIONNAME,t2.code,t1.name as parmname from BC_SITE_POLL t1 "
	    			  +" left join BC_SITE_INFO t2 on t1.SITE_ID=t2.code  WHERE 1=1  and t2.station_category='3' order by to_number(t2.code) asc";
	    	DataTable table = DbManager.getInstance().getOracleExecuter().executeQuery(sql);
	    	Set<String> keys = redis.keys("stationInfo:*:deviceInfo:*");
	    	Map<String,Response<String>> responses = new HashMap<String,Response<String>>(keys.size());	  
			for(String key : keys) {
				responses.put(key, pl.get(key));
			}	    
			pl.sync();
			for(int i=0;i<table.getRecordCount();i++){
				String stationCode = table.getString(i,"CODE");
				String NAME = table.getString(i,"PARMNAME");
				System.out.println("站点编号："+stationCode+";站点参数"+NAME);
				if(StringUtil.IsNotNullOrEmpty(responses.get("stationInfo:"+stationCode+":deviceInfo:"+NAME+"_last_MEAS").get())){
					String rtd=responses.get("stationInfo:"+stationCode+":deviceInfo:"+NAME+"_last_MEAS").get();				
					/*entityData.put("datatime",dateFormat.parseObject("2017-05-13 12:12:12"));//时间
					entityData.put("mn", stationCode );//MN号
					entityData.put("parmname", NAME);//参数
					entityData.put("flag", "N");//数据标识
					entityData.put("rtd",rtd);//参数值
					entityData.put("data_from","0");//数据来源标识：0设备上传，1人工监测，2人工修约(设备上传)，3人工修约(人工监测) 4系统修约(设备上传) 5系统修约(人工监测)
					ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
					ITable pTable = tableFactory.openTable("monitor_data");
					DataUtil.Create(pTable.getTableName(), entityData);*/
					String InsertSql="insert into monitor_data(guid,datatime,mn,parmname,flag,rtd,data_from)values"
							+ "(sys_guid(),to_date('2017-05-13 12:12:12','yyyy-mm-dd hh24:mi:ss'),'"+stationCode+"','"+NAME+"','N',to_number("+rtd+"),0)";
					DataUtil.ExecuteUpdateSql(InsertSql,null);
				}
				
			}			
			DataUtil.commitTransaction();
			start = System.currentTimeMillis();
			end = System.currentTimeMillis(); 
			System.out.println("withPipelineInsert: " + ((end - start)/1000.0) + "秒");
			redis.disconnect();	
		} catch (Exception e) {
			//log.error("实时数据入库错误", e);
			e.printStackTrace();
		}	
	}
}
